#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt

class SCA(object):

    def __init__(self, r, number):
        self.r = r
        self.number = number
        self.compress = []
        self.index = []
        self.L = [1]

    def compress_data(self, data):

        num = np.shape(data)[0]
        self.index.append(0)
        self.compress.append(data[0, :])
        for i in range(1, num, 1):
            compress_num = len(self.compress)
            min_dist = np.linalg.norm(data[i, :]-self.compress[0])
            min_j = 0
            for j in range(compress_num):
                dis = np.linalg.norm(data[i, :]-self.compress[j])
                if min_dist >= dis:
                    min_dist = dis
                    min_j = j
            if min_dist > self.r:
                self.compress.append(data[i, :])
                self.index.append(i)
            else:
                if len(self.L) > self.number:
                    self.compress.append(data[i, :])
                    self.index.append(i)
                    self.L = [1]
                    self.L.append(1)
                else:
                    self.L.append(1)
        return self.compress, self.index


if __name__ == '__main__':
    scale = 10
    delta = 1
    numb = 700
    x = np.arange(0, 2*np.pi, 0.01).reshape(-1, 1)
    y = np.array(np.sinc(x)*scale).reshape(-1, 1)
    data1 = np.hstack((x, y))
    s = SCA(delta, numb)
    compress_data, index = s.compress_data(data1)
    cx = x[index, :]
    cy = y[index, :]
    plt.grid(True, linewidth="0.5")
    plt.plot(x, y)
    plt.xlabel("x")
    plt.ylabel("y")
    plt.plot(cx, cy, ".")
    plt.show()

